//
//  RootViewController.m
//  DSYDouban
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "RootViewController.h"
#import "ActivityCell.h"
#import "ActivityModel.h"
#import "UIImageView+WebCache.h"
#import "DetailViewController.h"

@interface RootViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) NSMutableArray *activityArr;

@property (nonatomic, retain) UIImage *contentImage;

@property (nonatomic, retain) NSMutableArray *collectionArr;

@end

@implementation RootViewController

- (void)dealloc
{
    [_tableView release];
    [_activityArr release];
    [_contentImage release];
    [_collectionArr release];
    
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"活动";
    self.collectionArr = [NSMutableArray array];
    
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    [_tableView registerClass:[ActivityCell class] forCellReuseIdentifier:@"activityCell"];
    [_tableView release];
    
    // 数据的加载
    [self dataHandle];
}


- (void)dataHandle {
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"activity" ofType:@"plist"];
    
    NSArray *tempArr = [NSArray arrayWithContentsOfFile:path];
    
    self.activityArr = [NSMutableArray array];
    for (NSDictionary *dic in tempArr) {
        ActivityModel *model = [ActivityModel activityModel];
        [model setValuesForKeysWithDictionary:dic];
        
        [_activityArr addObject:model];
        
    }
    
//    NSLog(@"%@", _activityArr);
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ActivityCell *cell = [tableView dequeueReusableCellWithIdentifier:@"activityCell" forIndexPath:indexPath];
    
    
    ActivityModel *model = _activityArr[indexPath.row];
    NSString *beginTime = model.begin_time;
    NSString *endTime = model.end_time;
    
    cell.titleNameLabel.text = model.title;
    cell.start_to_endTimeLabel.text = [NSString stringWithFormat:@"%@ -- %@", [beginTime substringWithRange:NSMakeRange(5, 11)], [endTime substringWithRange:NSMakeRange(5, 11)]];
    cell.addressLabel.text = model.address;
    cell.category_nameLable.text = model.category_name;
    cell.wisher_countLabel.text = [NSString stringWithFormat:@"%@", model.wisher_count];
    cell.participant_countLable.text = [NSString stringWithFormat:@"%@", model.participant_count];
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:model.image]];

    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    
    detailVC.indexPath = indexPath;
    detailVC.collectionArr = [NSMutableArray arrayWithArray:_collectionArr];
    
    detailVC.block = ^(NSMutableArray *array) {
        _collectionArr = nil;
        self.collectionArr = [NSMutableArray arrayWithArray:array];
    };
    
    
    [self.navigationController pushViewController:detailVC animated:YES];
//    detailVC.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
//    [self presentViewController:detailVC animated:YES completion:^{
//        
//    }];
    
    [detailVC release];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _activityArr.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
